package com.eqdd.yiqidian.ui.gongneng.kehu;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.amap.api.maps2d.model.LatLng;
import com.eqdd.annotation.datasave.ToSave;
import com.eqdd.yiqidian.AddKehuActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.LianxirenListResultBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditTimeBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.map.SelectMapActivity;
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity;
import com.eqdd.yiqidian.ui.select.RichTextResult;
import com.eqdd.yiqidian.ui.select.SelectRichTextActivity;
import com.eqdd.yiqidian.utils.SingleChooseUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.eqdd.yiqidian.widget.ActionSheetDialog;
import com.luck.picture.lib.entity.LocalMedia;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.PostRequest;

import java.io.File;
import java.util.List;

public class AddFankuiRecordActivity extends SlimBaseMultiEditActivity {
    private AddKehuActivityCustom dataBinding;
    @ToSave
    String contacts;
    @ToSave
    String phone;
    private ActionSheetDialog fankuiTypeDialog;
    @ToSave
    int fankuiType;
    private String msg;
    private int type;
    private String cusid;
    @ToSave
    RichTextResult richTextResult;
    private Bundle bundle;
    private String clientName;
    private String latAndLong;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_add_kehu);
        initTopTitleBar(View.VISIBLE, "添加反馈记录");
        initTopRightText("添加", v -> {
            if (checkParam()) {
                PostRequest<HttpResult> params = OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.KEHU_ADD_FANKUI_RECORD)
                        .params("owner", user.getGuid())
                        .params("comid", user.getCompanyId())
                        .params("cusid", cusid)
                        .params("fbtitle", setParam(0))
                        .params("fbcontent", richTextResult == null ? null : richTextResult.getContent())
                        .params("fbtype", setParamNull(1))
                        .params("fberid", contacts)
                        .params("remindTime", setParamNull(5))
                        .params("addr", setParamNull(6))
                        .params("fberName", setParam(2))
                        .params("fberPhone", phone)
                        .params("fbTime", setParam(4));
                if (richTextResult != null) {
                    List<LocalMedia> selectMedia = richTextResult.getSelectMedia();
                    for (int i = 0, lenth = selectMedia.size(); i < lenth; i++) {
                        params.params("file" + i, new File(selectMedia.get(i).isCompressed() ? selectMedia.get(i).getCompressPath() : selectMedia.get(i).getPath()));
                    }
                }

                params.execute(new DialogCallBack<HttpResult>(AddFankuiRecordActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult> response) {
                        HttpResult httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            clearData();
                            setResult(Config.SUCCESS);
                            finish();
                        }
                    }
                });
            } else {
                ToastUtil.showShort(msg);
            }
        });
        type = getIntent().getIntExtra(Config.TYPE, -1);
        if (type != -1) {
            isRead = false;
        }
        cusid = getIntent().getStringExtra("cusid");
        clientName = getIntent().getStringExtra(Config.CLIENT_NAME);

    }


    private boolean checkParam() {
        if (TextUtils.isEmpty(datas.get(0).getContent())) {
            msg = "请输入反馈标题";
            return false;
        }
        if (TextUtils.isEmpty(datas.get(2).getContent())) {
            msg = "请选择反馈人";
            return false;
        }
        if (TextUtils.isEmpty(datas.get(4).getContent())) {
            msg = "请选择反馈时间";
            return false;
        }
        return true;
    }


    @Override
    protected RecyclerView initRecyclerView() {
        return dataBinding.recyclerView;
    }

    @Override
    public void itemClick(int p) {
        switch (slimAdapterEx.getDataItem(p).getPosition()) {
            case 3:
                bundle = new Bundle();
                bundle.putString(Config.TITLE, "备注");
                bundle.putSerializable(Config.RICH_TEXT_RESULT, richTextResult);
                startActivityForResult(SelectRichTextActivity.class, bundle, Config.SELECT_RICH_TEXT);
                break;
            case 1:
                if (fankuiTypeDialog == null) {
                    SingleChooseUtil.getChooseDialog(this, 30,
                            sheetDialog -> {
                                fankuiTypeDialog = sheetDialog;
                                fankuiTypeDialog.show();
                            }
                            , (position, strings) -> {
                                fankuiType = position;
                                datas.get(p).setContent(strings.get(position - 1));
                                slimAdapterEx.notifyItemChanged(p);
                            }

                    );
                } else {
                    fankuiTypeDialog.show();
                }
                break;
            case 6:
                startActivityForResult(SelectMapActivity.class, null, Config.SELECT_MAP);
                break;
            case 2:
                bundle = new Bundle();
                bundle.putString("cusid", cusid);
                startActivityForResult(ChooseLianxirenActivity.class, bundle, Config.CHOOSE_LIANXIREN);
                break;

        }

    }

    @Override
    protected void setEditBeans() {

        datas.add(new SlimEditEnterBean("*反馈标题", "", 0, "请输入(必填)"));
        datas.add(new SlimEditEnterBean("反馈类型", "", 1, true, "请选择"));
        datas.add(new SlimEditEnterBean("*反馈人", "", 2, true, "请选择（必选）"));
        datas.add(new SlimEditEnterBean("反馈内容", "", 3, true, "请填写"));
        datas.add(new SlimEditTimeBean("*反馈时间", "", 4, "请选择（必选）").setType(Config.YEAR_MONTH_DAY_TYPE));
        datas.add(new SlimEditTimeBean("反馈提醒时间", "", 5, "请选择").setType(Config.YEAR_MONTH_DAY_TYPE));
        datas.add(new SlimEditEnterBean("反馈地点", "", 6, true, "请选择"));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Config.SUCCESS) {
            switch (requestCode) {
                case Config.CHOOSE_LIANXIREN:
                    LianxirenListResultBean bean = (LianxirenListResultBean) data.getSerializableExtra("bean");
                    contacts = bean.getId() + "";
                    phone = bean.getCellphone();
                    slimAdapterEx.getData().get(2).setContent(bean.getName());
                    slimAdapterEx.notifyItemChanged(2);
                    break;
                case Config.SELECT_RICH_TEXT:
                    richTextResult = (RichTextResult) data.getSerializableExtra(Config.RICH_TEXT_RESULT);
                    datas.get(3).setContent(richTextResult.getContent());
                    slimAdapterEx.notifyItemChanged(3);
                    break;
                case Config.SELECT_MAP:
                    LatLng latLng = data.getParcelableExtra(Config.MAP_RESULT_LOCATION);
                    latAndLong = latLng.longitude + "," + latLng.latitude;
                    String map = data.getStringExtra(Config.MAP_RESULT_STRING);
                    datas.get(6).setContent(map);
                    slimAdapterEx.notifyItemChanged(6);
                    break;
            }
        }
    }
}
